package com.bakery.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bakery.common.Result;
import com.bakery.dto.SalesOrderDTO;
import com.bakery.entity.SalesLog;
import com.bakery.entity.SalesOrder;
import com.bakery.service.SalesLogService;
import com.bakery.service.SalesOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 销售订单Controller
 *
 * @author bakery
 */
@RestController
@RequestMapping("/sales")
public class SalesOrderController {
    
    @Autowired
    private SalesOrderService salesOrderService;
    
    @Autowired
    private SalesLogService salesLogService;
    
    /**
     * 创建销售订单
     */
    @PostMapping("/order")
    public Result<Void> createOrder(@Validated @RequestBody SalesOrderDTO dto) {
        salesOrderService.createOrder(dto);
        return Result.success();
    }
    
    /**
     * 分页查询订单
     */
    @GetMapping("/order/page")
    public Result<Page<SalesOrder>> getOrderPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(required = false) Integer status) {
        Page<SalesOrder> orderPage = salesOrderService.getOrderPage(page, size, orderNo, startTime, endTime, status);
        return Result.success(orderPage);
    }
    
    /**
     * 获取订单详情
     */
    @GetMapping("/order/{id}")
    public Result<SalesOrder> getOrderDetail(@PathVariable Long id) {
        SalesOrder order = salesOrderService.getOrderDetail(id);
        return Result.success(order);
    }
    
    /**
     * 取消订单（仅店长可操作）
     */
    @PostMapping("/order/cancel/{id}")
    @SaCheckRole("MANAGER")
    public Result<Void> cancelOrder(@PathVariable Long id, @RequestBody(required = false) Map<String, String> params) {
        String reason = params != null ? params.get("reason") : null;
        salesOrderService.cancelOrder(id, reason);
        return Result.success();
    }
    
    /**
     * 销售统计
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getSalesStatistics(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Map<String, Object> statistics = salesOrderService.getSalesStatistics(startTime, endTime);
        return Result.success(statistics);
    }
    
    /**
     * 分页查询销售日志
     */
    @GetMapping("/log/page")
    public Result<Page<SalesLog>> getSalesLogPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @RequestParam(required = false) Long employeeId) {
        Page<SalesLog> logPage = salesLogService.getSalesLogPage(page, size, orderNo, startTime, endTime, employeeId);
        return Result.success(logPage);
    }
}

